package com.ship.boats.service.impl;

import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ship.boats.domain.SpPortInformation;
import com.ship.boats.domain.dto.SavePortInfoDto;
import com.ship.boats.mapper.PortInformationMapper;
import com.ship.boats.service.PortInformationService;
import com.ship.common.core.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <b>PortInformationServiceImpl</b>
 *
 * @description: PortInformationServiceImpl <br>
 * @date: 2024/4/28 16:03 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class PortInformationServiceImpl extends ServiceImpl<PortInformationMapper, SpPortInformation> implements PortInformationService {
    private static final Logger logger = LoggerFactory.getLogger(PortInformationServiceImpl.class);

    @Override
    public int savePortInfoList(List<SavePortInfoDto> portList, Integer portType) {
        //查询所有港口信息
        List<SpPortInformation> portInformations = this.baseMapper.selectList(
                new LambdaQueryWrapper<SpPortInformation>()
                .eq(SpPortInformation::getPortType,portType)
                .isNotNull(SpPortInformation::getPortNameEnglish)
        );
        List<String> portNames = new LinkedList<>();
        if(portInformations.size() > 0){
            portNames = portInformations.stream().map(SpPortInformation::getPortNameEnglish).collect(Collectors.toList());
        }
        List<SavePortInfoDto> saveList = new LinkedList<>();
        for (SavePortInfoDto savePortInfoDto : portList) {
            String portName = savePortInfoDto.getPortNameEnglish();
            if(StringUtils.isBlank(portName) || portNames.contains(portName)){
                continue;
            }
            saveList.add(savePortInfoDto);
        }
        if(saveList.size() == 0){
            return 0;
        }
        List<SpPortInformation> insertPortInfo = new LinkedList<>();
        //同步港口、接收站数据
        if(portType == 1){
            //港口
            for (SavePortInfoDto savePortInfoDto : saveList) {
                SpPortInformation portInformation = new SpPortInformation();
                portInformation.setPortNameChinese(savePortInfoDto.getPortNameEnglish());
                portInformation.setPortNameEnglish(savePortInfoDto.getPortNameEnglish());
                portInformation.setPortCountry(savePortInfoDto.getPortCountry());
                portInformation.setLon(savePortInfoDto.getLon());
                portInformation.setLat(savePortInfoDto.getLat());
                if(savePortInfoDto.getPortNameChinese() != null){
                    portInformation.setPortNameChineseDel(savePortInfoDto.getPortNameChinese());
                }
                portInformation.setPortType("1");
                insertPortInfo.add(portInformation);
            }
        }else{
            //接收站
            for (SavePortInfoDto savePortInfoDto : saveList) {
                SpPortInformation portInformation = new SpPortInformation();
                portInformation.setPortNameEnglish(savePortInfoDto.getPortNameEnglish());
                portInformation.setPortNameChinese(savePortInfoDto.getPortNameEnglish());
                portInformation.setPortCountry(savePortInfoDto.getPortCountry());
                portInformation.setLon(savePortInfoDto.getLon());
                portInformation.setLat(savePortInfoDto.getLat());
                portInformation.setParentPortName(savePortInfoDto.getBelongPortNameEnglish());
                portInformation.setPortType("2");
                insertPortInfo.add(portInformation);
            }
        }
        if(insertPortInfo.size() > 0){
            this.saveBatch(insertPortInfo);
            return insertPortInfo.size();
        }
        return 0;
    }

}
